﻿using System.Windows.Forms;
using CalcSharp.Core.Containers;
using System;

namespace CalcSharp.MDIUI.Dialogs
{
    /// <summary>
    /// Matrix resize dialog
    /// </summary>
    public partial class ResizeMatrixDialog : Form
    {
        private double AspectRatio;

        public ResizeMatrixDialog(AMatrix m)
        {
            InitializeComponent();
            this.NumCols.Maximum = m.Columns*2;
            this.NumRows.Maximum = m.Rows*2;
            this.NumRows.Value = m.Rows;
            this.NumCols.Value = m.Columns;
            this.AspectRatio = (double)m.Columns / (double)m.Rows;
        }

        /// <summary>
        /// Gets or sets the number of rows
        /// </summary>
        public int Rows
        {
            get { return (int)this.NumRows.Value; }
            set { this.NumRows.Value = value; }
        }

        /// <summary>
        /// Gets or sets the number of columns
        /// </summary>
        public int Columns
        {
            get { return (int)this.NumCols.Value; }
            set { this.NumCols.Value = value; }
        }

        private void NumCols_ValueChanged(object sender, EventArgs e)
        {
            if (KeepAspect.Checked)
            {
                int val = (int)Math.Round((double)this.NumCols.Value / this.AspectRatio, 0);
                if (val > this.NumRows.Minimum) this.NumRows.Value = val;
            }
        }

        private void NumRows_ValueChanged(object sender, EventArgs e)
        {
            if (KeepAspect.Checked)
            {
                int val = (int)Math.Round(this.AspectRatio * (double)this.NumRows.Value, 0);
                if (val > this.NumCols.Minimum) this.NumCols.Value = val;
            }
        }
    }
}
